// DialogAddNewNode.cpp : implementation file
//

#include "stdafx.h"
#include "map863.h"
#include "DialogAddNewNode.h"
#include "MapParameters.h"

// CDialogAddNewNode dialog

IMPLEMENT_DYNAMIC(CDialogAddNewNode, CDialog)

CDialogAddNewNode::CDialogAddNewNode(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogAddNewNode::IDD, pParent)
	, m_szEdit1NodeName(_T(""))
	, m_szEdit1Address(_T(""))
{

}

CDialogAddNewNode::~CDialogAddNewNode()
{
}

void CDialogAddNewNode::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NODEPOSITION, m_NodePosition);
	DDX_Control(pDX, IDC_EDIT1, m_ctrlEdit1NodeName);
	DDX_Control(pDX, IDC_EDIT2, m_ctrlEdit1Comment);
	DDX_Control(pDX, IDC_EDIT3, m_ctrlEdit1Address);
	DDX_Text(pDX, IDC_EDIT1, m_szEdit1NodeName);
	DDX_Text(pDX, IDC_EDIT3, m_szEdit1Address);
}


BEGIN_MESSAGE_MAP(CDialogAddNewNode, CDialog)
//ON_BN_CLICKED(IDOK, &CDialogAddNewNode::OnBnClickedOk)
END_MESSAGE_MAP()


// CDialogAddNewNode message handlers

BOOL CDialogAddNewNode::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Ji
	// Set the limit of the text length
	m_ctrlEdit1NodeName.SetLimitText(19);
	m_ctrlEdit1Comment.SetLimitText(99);
	m_ctrlEdit1Address.SetLimitText(8);

	// TODO:  Add extra initialization here
	OKSelected= false;

	CString strCoordinator;
	CString strx, stry;
	strx.Format(_T("%4.1f"), (float)NodePosition.x/10);
	stry.Format(_T("%4.1f"), (float)NodePosition.y/10);
	strCoordinator=_T("(")+strx+_T(", ")+stry+_T(")");
	m_NodePosition.SetWindowTextW(strCoordinator);
/*
		TCHAR tempNodePosition[25]={0}, tempStr[11]={0};
		wcscat_s(tempNodePosition, _T("("));
		_itow_s(NodePosition.x, tempStr, 10, 10);
		wcscat_s(tempNodePosition, tempStr);
		wcscat_s(tempNodePosition, _T(", "));
		_itow_s(NodePosition.y, tempStr, 10, 10);
		wcscat_s(tempNodePosition, tempStr);
		wcscat_s(tempNodePosition, _T(")"));
*/ 
	if (NewNode) 
	{
		SetWindowText(_T("Add a new node"));
	}
	else
	{
		TCHAR tempWChar[MAXNODENAMELENGTH+1]={0};
		char tempChar[MAXNODENAMELENGTH]={0};
		unsigned char tempi;

		SetWindowText(_T("Property"));
		SetDlgItemText(IDC_EDIT2, NodeComment);
		MultiByteToWideChar(CP_ACP, 0, NodeName, -1, tempWChar, 20);
		SetDlgItemText(IDC_EDIT1, tempWChar);
		
		
		TRACE("\n IEEE ADDRESS:");
		
		for (int i=0;i<8;i++)
			TRACE("%d, ", NodeIEEEAddress[i]);
		TRACE("\n");

		int j=0;
		for (int i=0;i<8;i++)
		{				
			tempi=NodeIEEEAddress[i]/16;
			if (tempi<=9 && tempi>=0)
				tempChar[j]=tempi+'0';
			else //if (NodeIEEEAddress[j]<=15 && NodeIEEEAddress[j]>=10)
				tempChar[j]=tempi+'A'-10;
		//	else
		//		tempChar[j]='0';
			j++;

			tempi=NodeIEEEAddress[i]%16;
			if (tempi<=9 && tempi>=0)
				tempChar[j]=tempi+'0';
			else //if (NodeIEEEAddress[j]<=15 && NodeIEEEAddress[j]>=10)
				tempChar[j]=tempi+'A'-10;
		//	else
		//		tempChar[j]='0';
			j++;
		}

		TCHAR tempWChar2[18]={0};
		MultiByteToWideChar(CP_ACP, 0, tempChar, -1, tempWChar2, 16);
		SetDlgItemText(IDC_EDIT3, (CString)tempWChar2);
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALS
}

//Ji
int CDialogAddNewNode::StrLenEx(CString csText)
{
	int nByte= csText.GetLength();
	int nWord= 0;
	for (int i=0;i<nByte;i++)
	{
		nWord++;
		if( IsDBCSLeadByte(csText[i]))
			i++;
	}
	return nWord;
}

bool CDialogAddNewNode::TestIllegalCharacter(CString csText,int nWord)
{
	//int nWord= csText.GetLength();
	for(int i=0;i<nWord;i++)
	{
		//int ascii= csText.GetAt(i);
		if(csText[i]<48 || (csText[i]>57&&csText[i]<65) || (csText[i]>70&&csText[i]<97) || csText[i]>102)
			return false;
	}
	return true;
}

void CDialogAddNewNode::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class

	//Ji
	//Testify the node name and the address
	m_ctrlEdit1NodeName.GetWindowText(m_szEdit1NodeName);
	m_ctrlEdit1Address.GetWindowText(m_szEdit1Address);
	int nWord= StrLenEx(m_szEdit1Address); 

	if(m_szEdit1NodeName=="")
	{
		MessageBox(L"Please enter the Node Name!",L"Warning!");
		m_ctrlEdit1NodeName.SetFocus();   //Get the focus to enter the node name
		return;
	}
	else if(m_szEdit1Address=="")
	{
		MessageBox(L"Please enter the Node Address!",L"Warning!");
		m_ctrlEdit1Address.SetFocus();    //Get the focus to enter the address
		return;
	}
	else if( !TestIllegalCharacter(m_szEdit1Address,nWord))
	{
		MessageBox(L"Please enter the Correct Address!",L"Warning!");
		m_ctrlEdit1Address.SetWindowTextW(L"");
		m_ctrlEdit1Address.SetFocus();    //Get the focus to enter the address
		return;
	}


	// TODO: Add your specialized code here and/or call the base class
	TCHAR tempWChar[100]={0};
	char tempChar[200]={0};

	//Retrieve Node name and transfer it to UN_UNICODE char
	GetDlgItemTextW(IDC_EDIT1, tempWChar, MAXNODENAMELENGTH);
	TRACE(tempWChar);
	WideCharToMultiByte(CP_ACP, 0, tempWChar, -1, tempChar, 200, NULL, NULL);
//	for (int i=0; i<MAXNODENAMELENGTH; i++)
//		NodeName[i]=tempChar[i];
	tempChar[MAXNODENAMELENGTH-1]=0;
	strcpy_s(NodeName, MAXNODENAMELENGTH, tempChar);

	GetDlgItemText(IDC_EDIT2, NodeComment, MAXNODECOMMENTLENGTH);

	TCHAR tempWChar2[18];
	char tempChar2[18];
	unsigned char tempi[2];
	GetDlgItemText(IDC_EDIT3, tempWChar2, 18);
	WideCharToMultiByte(CP_ACP, 0, tempWChar2, -1, tempChar2, 18, NULL, NULL);
	int j=0;
	for (int i=0;i<8;i++)
	{	
		if (tempChar2[j]<='9' && tempChar2[j]>='0')
			tempi[1]=tempChar2[j]-'0';
		else if (tempChar2[j]<='F' && tempChar2[j]>='A')
			tempi[1]=tempChar2[j]+10-'A';
		else
			tempi[1]=0;
		j++;

		if (tempChar2[j]<='9' && tempChar2[j]>='0')
			tempi[0]=tempChar2[j]-'0';
		else if (tempChar2[j]<='F' && tempChar2[j]>='A')
			tempi[0]=tempChar2[j]+10-'A';
		else
			tempi[0]=0;
		j++;

		NodeIEEEAddress[i]=tempi[1]*16+tempi[0];
	}

	OKSelected=true;

	CDialog::OnOK();
}